import axios from 'axios'
import Api from '../config/api'

const Com = {
    //验证全是空格的情况
    validateSpace: { pattern: /\S+/, message: '不能为空' },
    /**
     * 格式化日期
     * @desc 对于小于10的数字一位填0
     * @param {Number} data 传入的数字
     * @return {String} 格式化后的数字字符串
     */
    formatDate: function (data) {
        return data < 10 ? '0' + data : data
    },

    /**
     * 计算日期区间
     * @param {String} type 区间类型
     * @return {Object} 带有属性start和end的区间对象
     */
    computeDateGap: function (type) {
        let date = { startTime: '', endTime: '' };
        let now = new Date();
        let year = now.getFullYear();
        let fYear = parseInt(year, 10);
        let month = this.formatDate(now.getMonth() + 1);
        let fMonth = parseInt(month, 10);
        let day = this.formatDate(now.getDate());

        if (type === '今年') {
            date.startTime = year + '-01-01';
            date.endTime = year + '-' + month + '-' + day;
        }
        else if (type === '半年') {
            if (fMonth < 6) {
                let month2 = fMonth + 7;
                date.startTime = (fYear - 1) + '-' + month2 + '-01';
            }
            else {
                let month2 = fMonth - 5;
                if (month2 < 10) {
                    month2 = '0' + month2;
                }
                date.startTime = year + '-' + month2 + '-01';
            }
            date.endTime = year + '-' + month + '-' + day;
        }
        else if (type === '近三月') {
            if (fMonth < 3) {
                let month2 = fMonth + 11;
                date.startTime = (fYear - 1) + '-' + month2 + '-01';
            }
            else {
                let month2 = fMonth - 2;
                if (month2 < 10) {
                    month2 = '0' + month2;
                }
                date.startTime = year + '-' + month2 + '-01';
            }
            date.endTime = year + '-' + month + '-' + day;
        }
        else if (type === '上月') {
            if (fMonth === 1) {
                let month2 = 12;
                date.startTime = (fYear - 1) + '-' + month2 + '-01';
            }
            else {
                let month2 = fMonth - 1;
                if (month2 < 10) {
                    month2 = '0' + month2;
                }
                date.startTime = year + '-' + month2 + '-01';
            }
            let preMonth = new Date(year, now.getMonth(), 0)
            let month3 = this.formatDate(preMonth.getMonth() + 1);
            date.endTime = preMonth.getFullYear() + '-' + month3 + '-' + preMonth.getDate();
        }
        else {
            date.startTime = year + '-' + month + '-01';
            date.endTime = year + '-' + month + '-' + day;
        }

        return date
    },

    /**
     * 字符串缩减
     * @desc 对字符串进行指定位数的缩减，并拼省略号
     * @param {String} data 需要处理的字符串
     * @param {Number} num 指定位数
     * @return {String} 返回处理过的字符串
     */
    abbrData: function (data, num) {
        return data.length > num ? (data.toString().slice(0, num) + '...') : data;
    },

    /**
     * 获取当天日期
     * @return {String} YYYY-MM-DD
     */
    getToday: function () {
        let now = new Date();
        let day, month, year;
        day = now.getDate();
        month = now.getMonth() + 1;
        year = now.getFullYear();
        return year + '-' + this.formatDate(month) + '-' + this.formatDate(day);
    },

    /**
     * 遍历数组获取key对应的名称
     * @param {Array} arr 数组
     * @param {String} key 查询的key
     * @return {String} 返回名称
     */
    keyToName(arr, key) {
        let name = '';
        arr.forEach((item) => { if (parseInt(key, 10) === parseInt(item.index, 10)) { name = item.desc } });
        return name;
    },
    /**
     * 获取下拉列表
     * @param {String} param 参数
     * @param {Array} arr 要赋值的数组
     * @param {Boolean} all 是否添加全部项
     * @return {Promise}
     */
    getSelection(param, arr, all = true) {
        return new Promise((resolve) => {
            axios.post(Api['commonSelect'] + '?em=' + param).then((res) => {
                if (res) {
                    let data = JSON.parse(JSON.stringify(res.data));
                    if (all) {
                        data.unshift({ index: '', desc: '全部' });
                    }
                    this.setState({
                        [arr]: data
                    }, () => { resolve() })
                }
            }).catch((error) => {
                console.log(error)
            })
        })
    },
    /**
    * 获取产品下拉列表
    * @param {String} param 参数
    * @param {Array} arr 要赋值的数组
    * @return {Promise}
    */
    getOperSelection(param, arr) {
        return new Promise((resolve) => {
            axios.post(Api['operatorSelect'] + '?operator=' + param).then((res) => {
                if (res) {
                    let data = JSON.parse(JSON.stringify(res.data));
                    this.setState({
                        [arr]: data
                    }, () => { resolve() })
                }
            }).catch((error) => {
                console.log(error)
            })
        })
    }
}

export default Com